<?php

namespace App\Store\Controllers\Basic;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Basic\OrderRemark;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Validate\Basic\OrderRemark as ValidateBasicOrderRemark;

/**
 * 备注信息
 */
class OrderRemarkController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = OrderRemark::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = OrderRemark::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids', 'remark_title']);

        $Validate = new ValidateBasicOrderRemark();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);

        $store_ids=$req_data['store_ids'];
        $store_ids=is_array($store_ids)?$store_ids:(is_array(json_decode($store_ids,true))?json_decode($store_ids,true):[]);


        if(count($store_ids)<=0){

            return response()->json(['code' => '001', 'msg' => '无效门店参数']);
        }

        $data_data=[];
        foreach($store_ids as $store_id){
            $storeinfo=SiteStore::where(['id'=>$store_id,'cp_id'=>$accountinfo['cp_id']])->first();
            if(!$storeinfo){
                return response()->json(['code' => '001', 'msg' => '无效门店参数']);
            }
            $isexist = OrderRemark::where([['remark_title', $req_data['remark_title']],['store_id',$store_id], ['cp_id', $accountinfo['cp_id']]])->first();
            if ($isexist) {
                return response()->json(['code' => '001', 'msg' => $storeinfo->store_name.'备注名称不允许重复']);
            }
    
            $data=$req_data;
            unset($data['store_ids']);
            $data['store_id']=$store_id;
            $data['create_account_id']=$accountinfo['id'];
            $data['update_account_id']=$accountinfo['id'];
            $data['create_account_real_name'] = $accountinfo['real_name'];
            $data['update_account_real_name'] = $accountinfo['real_name'];
            $data['cp_id'] = $accountinfo['cp_id'];
            $data['create_at'] = time();
            $data['update_at'] = time();
            $data_data[]=$data;
        }

        OrderRemark::insert($data_data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'remark_title']);

        $Validate = new ValidateBasicOrderRemark('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $info = OrderRemark::where([['id', $req_data['id']], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $isexist = OrderRemark::where([['id', '<>', $req_data['id']],['store_id',$info->store_id] ,['remark_title', $req_data['remark_title']], ['cp_id', $accountinfo['cp_id']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '当前门店备注名称不允许重复']);
        }

        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        $data['update_account_id'] = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        OrderRemark::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = OrderRemark::where([['id', $id], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        OrderRemark::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

}
